/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ourMessage;

import java.net.ServerSocket;
import ChateoController.*;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * This classe is aimed in sending TCP Files
 *
 * @author adrian
 */
public class ConnexionTCPServer extends Thread {

    private static ConnexionTCPServer instance = null;
    private ServerSocket sockServ;
    private Socket sockClient;
    private int portServ;
    private FileTransfer fileTransferState;
    private Controller fileControl;
    private BufferedOutputStream buffOutStream;
    private ObjectOutputStream objOutStream;

    /**
     *
     * @param fileControl will be used for files transfer
     */
    private ConnexionTCPServer(Controller fileControl) {
        try {
            this.fileControl = fileControl;
            sockServ = new ServerSocket(0);
        } catch (IOException ex) {
            System.out.println("Failed to create the Server's socket in the private constructor \n");
            Logger.getLogger(ConnexionTCPServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is aimed in getting a unique instance of the class
     *
     * @param fileControl will be used for file transfer
     * @return the unique instance of the class
     */
    public final static ConnexionTCPServer getInstance(Controller fileControl) {
        synchronized (ConnexionTCPServer.class) {
            if (ConnexionTCPServer.instance == null) {
                ConnexionTCPServer.instance = new ConnexionTCPServer(fileControl);
            }
        }
        return ConnexionTCPServer.instance;
    }

    /**
     * This method is aimed in reseting the Class' instance
     */
    public final static void resetInstance() {
        ConnexionTCPServer.instance = null;
    }

    @Override
    public void run() {
        try {
            this.sockClient = sockServ.accept();
            this.buffOutStream = new BufferedOutputStream(sockClient.getOutputStream());
            this.objOutStream = new ObjectOutputStream(buffOutStream);
            
            Message msg ;
            while(this.fileTransferState.getStatus() == FileTransfer.INPROGRESS){
                //  Send the FILE
            }
            
        } catch (IOException ex) {
            Logger.getLogger(ConnexionTCPServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /**
     * This method is aimed in closing  sockets and the stream
     */
    public void close(){
        try {
            this.objOutStream.close();
            this.buffOutStream.close();
            this.sockClient.close();
            this.sockServ.close();
        } catch (IOException ex) {
            Logger.getLogger(ConnexionTCPServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Getter for file emission state
     * @return the File's transfer state 
     */    
    public FileTransfer getFileTransferState() {
        return fileTransferState;
    }

    /**
     * Setter for file emission state
     * @param fileTransferState is the new state assigned to a file
     */
    public void setFileTransferState(FileTransfer fileTransferState) {
        this.fileTransferState = fileTransferState;
    }

    public int getPortServ() {
        return this.sockServ.getLocalPort();
    }
    
    

}
